/********************************** (C) COPYRIGHT *******************************
* File Name          : main.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2021/06/06
* Description        : Main program body.
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/

/*
 *@Note
 USART Print debugging routine:
 USART1_Tx(PA9).
 This example demonstrates using USART1(PA9) as a print debug port output.

*/

#include "debug.h"
#include "b5wc.h"


#include "tim3.h"
#include "Sys_Config.h"
#if MD_USD_SALVE
#include "MDS_RTU_APP_1.h"
#else
#include "MDM_RTU_APP.h"
#include "MDM_RTU_Fun.h"
#include "MDM_RTU_User_Fun.h"
#endif


u16 mem_size[2]={0};
u32 uid[3]={0};
u16 rgb_value[3]={0};





#if MD_RTU_USED_OS
PTASK_TCB task0;
PTASK_TCB task1;
PTASK_TCB task2;
extern Modbus_RTU_CB modbusRWRTUCB;
extern Modbus_RTU_CB modbusRWRTUCB1;
void mdTestTask0(void *arg){
    uint16 temp;
    for(;;){
        if(ERR_RW_FIN==MDM_RTU_ReadHoldReg(&modbusRWRTUCB,1,0,1)){
            MDM_RTU_ReadRegs(modbusRWRTUCB.pModbus_RTU,0,1, (&temp),HOLD_REGS_TYPE,0x1);
        }
        OSTaskDelay(500);
    }
}
void mdTestTask1(void *arg){
    uint16 temp=0;
    for(;;){
        if(ERR_RW_FIN==MDM_RTU_WriteSingleReg(&modbusRWRTUCB1,2,0,temp)){
            //MDM_RTU_ReadRegs(modbusRWRTUCB1.pModbus_RTU,0,1, (&temp),HOLD_REGS_TYPE,2);
        }
        temp++;
        OSTaskDelay(500);
    }
}
#endif

int main(void)
 {
     #if MD_RTU_USED_OS
    OSInit();
        #endif
    //delay_init();
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    Delay_Init();
    USART_Printf_Init(9600);
    b5cw_init();

#if MD_USD_SALVE
        MDS_RTU_APPInit_1();
#else
        MDM_RTU_APPInit();

#endif
    TIM3_Int_Init(96-1,100-1);



    #if MD_RTU_USED_OS
    task1=OSCreateTask(mdTestTask0,NULL,6,256);
    task2=OSCreateTask(mdTestTask1,NULL,6,256);
    SysTick_Config(SystemCoreClock / OS_TICKS_PER_SEC);
    OSStart();
    #endif
    while(1){
        #if !MD_RTU_USED_OS
            #if MD_USD_SALVE
            MDS_RTU_Loop_1();
            #else
            MDM_RTU_Loop();
            MDM_RW_CtrlLoop();
            #endif
        #endif
    }
}


